/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.user.comment.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.param.*;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.DateParam;
import com.yami.shop.common.util.DateUtils;
import com.yami.shop.dao.UserExtensionMapper;
import com.yami.shop.user.comment.model.UserLevelLog;
import com.yami.shop.user.comment.dao.UserLevelLogMapper;
import com.yami.shop.user.comment.service.UserLevelLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Service
@AllArgsConstructor
public class UserLevelLogServiceImpl extends ServiceImpl<UserLevelLogMapper, UserLevelLog> implements UserLevelLogService {

    private final UserLevelLogMapper userLevelLogMapper;
    private final UserExtensionMapper userExtensionMapper;

    @Override
    public Page<UserLevelLog> getPage(Page<UserLevelLog> page, UserLevelLog userLevelLog) {
        return userLevelLogMapper.getPage(page,userLevelLog);
    }

    @Override
    public Integer getMaxLevelByUserId(String userId) {
        return userLevelLogMapper.getMaxLevelByUserId(userId);
    }

    @Override
    public CustomerMemberParam countMemberNum(CustomerReqParam param) {
        List<DateParam> everyDays = DateUtils.findEveryDays(param.getStartTime(), param.getEndTime());
        CustomerMemberParam resParam = new CustomerMemberParam();
        List<CustomerMemberTrendParam> resList = new ArrayList<>();
        if (everyDays.size() == 1 ) {
            Date startTime = everyDays.get(0).getStartTime();
            Date beforeDate = DateUtils.getBeforeDate(startTime);
            everyDays = DateUtils.findEveryDays(beforeDate, param.getEndTime());
        }
        Integer userNum = null;
        for (DateParam everyDay : everyDays) {
            CustomerMemberTrendParam res = new CustomerMemberTrendParam();
            param.setDateTime(null);
            param.setStartTime(null);
            param.setEndTime(null);
            res.setCurrentDay(DateUtils.dateToStrYmd(everyDay.getEndTime()));
            param.setDateTime(everyDay.getStartTime());
            //累计会员
            res.setAccumulate(userExtensionMapper.countMemberByParam(param));
            if (Objects.isNull(userNum)){
                param.setDateTime(DateUtil.offsetDay(everyDay.getStartTime(),-1));
                userNum = userExtensionMapper.countMemberByParam(param);
                param.setDateTime(everyDay.getStartTime());
                res.setNewAddMember(res.getAccumulate() - userNum);
            }else {
                int num = res.getAccumulate() - userNum;
                if (num < 0){
                    res.setNewAddMember(0);
                }else {
                    res.setNewAddMember(num);
                }
            }
            userNum = res.getAccumulate();
            // 升级会员数
            param.setDateTime(null);
            param.setStartTime(everyDay.getStartTime());
            param.setEndTime(everyDay.getEndTime());
            res.setGrowthMember(userLevelLogMapper.countGrowthMember(param));
            // 付费会员数
            param.setDateTime(everyDay.getEndTime());
            res.setPayMember(userExtensionMapper.countPayMemberByParam(param));
            resList.add(res);
        }
        resList.sort(Comparator.comparing(CustomerMemberTrendParam::getCurrentDay));
        CustomerMemberTrendParam trendParam = resList.get(resList.size() - 1);
        CustomerMemberTrendParam first = resList.get(0);
        resParam.setAccumulate(trendParam.getAccumulate());
        resParam.setAccumulateRate(divRate(resParam.getAccumulate()-first.getAccumulate(),first.getAccumulate(),4));
        resParam.setNewAddMember(trendParam.getNewAddMember());
        resParam.setNewAddMemberRate(divRate(resParam.getNewAddMember()-first.getNewAddMember(),first.getNewAddMember(),4));
        resParam.setGrowthMember(trendParam.getGrowthMember());
        resParam.setGrowthMemberRate(divRate(resParam.getGrowthMember()-first.getGrowthMember(),first.getGrowthMember(),4));
        resParam.setPayMember(trendParam.getPayMember());
        resParam.setPayMemberRate(divRate(resParam.getPayMember()-first.getPayMember(),first.getPayMember(),4));
        resParam.setList(resList);
        return resParam;
    }

    @Override
    public UserLevelLog getMaxCrtTimeByUserId(String userId) {
        return userLevelLogMapper.getMaxCrtTimeByUserId(userId);
    }

    private Double divRate(Integer a, Integer b,Integer scale) {
        if (Objects.isNull(b) || b == 0 || Objects.isNull(a)) {
            return 0.0;
        }
        return Arith.div(a,b,scale);
    }
}
